//
//  main.m
//  vcf
//
//  Created by admid on 16-5-9.
//  Copyright (c) 2016年 oc_5-9. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Penson.h"

int main(int argc, const char * argv[]){
    @autoreleasepool {
        
        // insert code here...
        NSString * path = @"/Users/admid/Desktop/苹果1512宋10/00001.vcf";
        NSError *err;
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err != nil) {
            NSLog(@"%@",err);
            
        }
    
        NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
        for (NSString *line in arr){
            if ([line hasPrefix:@"BEGIN"]){
                NSLog (@"开始新记录 ");
                dict=[NSMutableDictionary new];
            }
            if ([line hasPrefix:@"END"]){
            
                NSLog(@" 这条记录结束了 ");
                [res addObject:dict];
            }
            if ([line hasPrefix:@"TEL"]){
            
                NSLog(@" 找到一条通话记录 %@ ",line);
                NSArray *tels = [line componentsSeparatedByString:@";"];
                
                NSString *last = tels.lastObject;
                
                last = [last substringFromIndex:5];
                
                NSLog(@"tel:%@",last);
                
                dict[@"tel"]=last;
                
                
            }
            if ([line hasPrefix:@"FN"]){
            
                NSLog(@"找到FN,名字:%@ ",line);
                NSArray *names = [line componentsSeparatedByString:@":"];
                NSString *name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"name:%@",name);
                dict[@"name"] = name;
                
            }
                    }
        
        NSLog(@"%@",res);
        [res writeToFile:@"/Users/admid/Desktop/苹果1512宋10/res.txt" atomically:NO];
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        [data writeToFile:@"/Users/admid/Desktop/苹果1512宋10/res2.txt" atomically:NO];
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSMutableArray *res2 = [NSMutableArray new];
        for (NSDictionary *dict in res) {
            Penson *p = [Penson new];
            [p setValuesForKeysWithDictionary:dict];
            [res2 addObject:p];
        
        }
        NSLog(@"%@",res2);
    
    }
    return 0;
}

